import { defineStore } from "pinia";

export const useUserStore = defineStore("user", {
  state: () => {
    return {
      longitude: 0, // 经度
      latitude: 0, // 纬度
    };
  },

  actions: {
    // 获取当前的定位
    getLocation(): Promise<{
      longitude: number;
      latitude: number;
    }> {
      return new Promise((resolve, reject) => {
        uni.getLocation({
          type: "gcj02",
          success: (res) => {
            // res 就是包含有经纬度字段的一个对象
            this.longitude = res.longitude;
            this.latitude = res.latitude;

            resolve(res);
          },
          fail: (error) => {
            reject(error);
          },
        });
      });
    },
  },
});
